<?php

namespace app\admin\controller\zhongzhuan\feiyu;

use app\common\controller\Backend;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Huichuan extends Backend
{

    /**
     * Huichuan模型对象
     * @var \app\admin\model\zhongzhuan\feiyu\Huichuan
     */
    protected $model = null;
    
    protected $wechat_list = null;
    
    protected $yixiang_list = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\zhongzhuan\feiyu\Huichuan;
        $this->assignconfig("clue_list", $this->model->getClueList());
        
        $this->assignconfig("step_list", $this->model->getStages());
        $this->assignconfig("wechat_list", $this->model->getLabelConfig('微信情况'));
        $this->assignconfig("yixiang_list", $this->model->getLabelConfig('意向程度'));
        $this->wechat_list = $this->model->getLabelConfig('微信情况');
        $this->yixiang_list = $this->model->getLabelConfig('意向程度');
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    
    /**
     * 查看
     */
    public function index()
    {
//        $crmids = '10161781692';
        $request_data = $this->request->param();
        $crmids = $request_data['crmids'];
//        $ids = $request_data['ids'];
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
//            $request_data = $this->request->param();
//            $filter = json_decode($request_data['filter'],true);
//            $op = json_decode($request_data['op'],true);
//            if(!empty($filter['wechat']) || !empty($filter['yixiang'])){
//                $filter[]
//            }
            
           
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
       
          
            $list = $this->model
                ->where('crmId',$crmids)
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit)->each(function($item){
                    $ec_info = [];
                    if(!empty($item['crmId'])){
                        $ec_info  = db('ec_aiuser_to_ecuser_feiyu')->where('crmId',$item['crmId'])->find();
                    }
                    $item_label_list = [];
                    if(!empty($item['label'])){
                        $item_label_list = [];
                        $label_list = explode(',', $item['label']);
                        foreach ($label_list as $v){
                            if(array_key_exists($v, $this->wechat_list)){
                                $item_label_list[] = $this->wechat_list[$v];
                            }else if(array_key_exists($v, $this->yixiang_list)){
                                $item_label_list[] = $this->yixiang_list[$v];
                            }
                        }
                        
                    }
                    $item['label_list'] = $item_label_list;
                    $item['label_list_txt'] = implode(',', $item_label_list);
                    
                    $item['ec_info'] = $ec_info;
                    return $item;
                });

//                var_dump(db('ec_aiuser_to_ecuser_feiyu_log')->getLastSql());exit;
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        $this->assignconfig('crmids',$crmids); //水印
//        $this->assignconfig('ids',$ids); //水印
        return $this->view->fetch();
    }
    
    
   
    
    
    


}
